package com.hifar.test.structure.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.fw.utils.bean.BusiConst;
import com.hifar.log.AutoLogUtils;
import com.hifar.model.VO.QueryParamsVO;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.test.structure.pojo.HfEnvResPersonTrainCheck;
import com.hifar.test.structure.pojo.HfEnvResPersonTrainRecord;
import com.hifar.test.structure.pojo.VO.HfEnvResPersonTrainRecordSaveVO;
import com.hifar.test.structure.service.IHfEnvResPersonTrainCheckService;
import com.hifar.test.structure.service.IHfEnvResPersonTrainRecordService;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.RequestConver;
import com.hifar.sys.SystemUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author zhuWeiWei
 * @description 体系管理-培训记录
 * @date 2025/2/14 11:54
 */
@RestController
@RequestMapping("/api/busi/test/envResPersonTrainRecord")
@Api(tags = "体系管理-培训记录")
public class HfEnvResPersonTrainRecordController {

    @Autowired
    private IHfEnvResPersonTrainRecordService envResPersonTrainRecordService;
    @Autowired
    private IHfEnvResPersonTrainCheckService envResPersonTrainCheckService;
    @Autowired
    private AutoLogUtils autoLogUtils;

    @ApiOperation("新增")
    @PostMapping("/add")
    public BusiResponse add(@RequestBody @Validated HfEnvResPersonTrainRecordSaveVO envResPersonTrainRecordSaveVO) {
        envResPersonTrainRecordService.add(envResPersonTrainRecordSaveVO);

        String trainTheme = envResPersonTrainRecordSaveVO.getTrainTheme();
        autoLogUtils.addLog("环境试验-培训记录", String.format("新增培训记录:【%s】", trainTheme));
        return BusiResponse.OK();
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public BusiResponse edit(@RequestBody @Validated HfEnvResPersonTrainRecordSaveVO envResPersonTrainRecordSaveVO) {
        envResPersonTrainRecordService.edit(envResPersonTrainRecordSaveVO);

        String trainTheme = envResPersonTrainRecordSaveVO.getTrainTheme();
        autoLogUtils.modifyLog("环境试验-培训记录", String.format("编辑培训记录:【%s】", trainTheme));
        return BusiResponse.OK();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public BusiResponse delete(@RequestBody List<String> idList) {
        envResPersonTrainRecordService.removeByIds(idList);
        autoLogUtils.modifyLog("环境试验-培训记录", String.format("删除%d培训记录", idList.size()));
        return BusiResponse.OK();
    }

    @ApiOperation("列表查询")
    @PostMapping("/listPage")
    public BusiResponse listPage(@RequestBody @Validated QueryParamsVO queryParamsVO) {
        QueryParamBean queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvResPersonTrainRecord.class);
        QueryWrapper queryWrapper = queryParamBean.getQueryWrapper();
        Page page = queryParamBean.getPage();
        Page pageData = envResPersonTrainRecordService.page(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }


    @ApiOperation("查询详情")
    @GetMapping("/queryById")
    public BusiResponse queryById(@RequestParam("id") String id) {
        HfEnvResPersonTrainRecord hfEnvResPersonTrainRecord = envResPersonTrainRecordService.getById(id);
        Assert.notNull(hfEnvResPersonTrainRecord, "数据不存在!");
        HfEnvResPersonTrainRecordSaveVO hfEnvResPersonTrainRecordSaveVO = new HfEnvResPersonTrainRecordSaveVO();
        BeanUtils.copyProperties(hfEnvResPersonTrainRecord, hfEnvResPersonTrainRecordSaveVO);
        List<HfEnvResPersonTrainCheck> hfEnvResPersonTrainChecks = envResPersonTrainCheckService.listByRecordId(id);
        hfEnvResPersonTrainRecordSaveVO.setTrainCheckList(hfEnvResPersonTrainChecks);
        return BusiResponse.OK(hfEnvResPersonTrainRecordSaveVO);
    }


    @PostMapping("/exportExcel")
    @ApiOperation(value = "培训记录功能:导出数据")
    public Object exportExcel(HttpServletRequest request) {
        Map<String, Object> pageInfo = RequestConver.getPageInfo(request);
        if (pageInfo == null) {
            return BusiResponse.error("参数有误！");
        }
        BusiResponse export = envResPersonTrainRecordService.exportExcel(pageInfo);
        if (export.getCode() == BusiConst.CODE_SUCCESS) {
            String encode = null;
            try {
                encode = URLEncoder.encode("培训记录.xlsx", "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/vnd.ms-excel");
            headers.add("Access-Control-Expose-Headers", "Content-Disposition");
            headers.add("Content-Disposition", "filename=" + encode);
            autoLogUtils.exportLog("环境试验-培训记录", "导出培训记录列表");
            return ResponseEntity.status(HttpStatus.OK).headers(headers).body((byte[]) export.getData());
        }
        return export;
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "培训记录功能:导入excel数据")
    public BusiResponse importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return BusiResponse.error("文件为空！");
        } else {
            if (ExcelTemplateUtil.isExcelFile(file)) {
                autoLogUtils.importLog("环境试验-培训记录", "导入培训记录列表");
                return envResPersonTrainRecordService.importExcel(file);
            } else {
                return BusiResponse.error("请上传excel文件！");
            }
        }
    }
}
